package com.huanglj.taobaounion.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.huanglj.taobaounion.R;
import com.huanglj.taobaounion.model.domain.CategoriesById;
import com.huanglj.taobaounion.utils.LogUtil;
import com.huanglj.taobaounion.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomePagerContentAdapter extends RecyclerView.Adapter<HomePagerContentAdapter.InnerHolder> {

    private static final String TAG = "HomePagerContentAdapter";
    List<CategoriesById.DataBean> mData = new ArrayList<>();
    private OnItemClickListener mListener;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_page_result, parent, false);
        InnerHolder innerHolder = new InnerHolder(view);

        return innerHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        holder.setData(mData.get(position));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(List<CategoriesById.DataBean> contents) {
        mData.clear();
        mData.addAll(contents);
        notifyDataSetChanged();
    }

    public void addData(List<CategoriesById.DataBean> contents) {
        //添加前拿到size
        int addBeforeSize = mData.size();
        mData.addAll(contents);
        int addSize = contents.size();
        //局部更新
        notifyItemRangeChanged(addBeforeSize,addSize);
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.item_image)
        ImageView cover;
        @BindView(R.id.item_main_text)
        TextView mainText;
        @BindView(R.id.item_price_text)
        TextView priceText;
        @BindView(R.id.item_preferential_price_text)
        TextView preferentialText;
        @BindView(R.id.item_goods_original_prise)
        TextView originalPrise;
        @BindView(R.id.item_volume_text)
        TextView volumeText;
        @BindView(R.id.item_main)
        LinearLayout mLinearLayout;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

        public void setData(CategoriesById.DataBean data) {
            //标题
            mainText.setText(data.getTitle());
            //图片
            ViewGroup.LayoutParams layoutParams = cover.getLayoutParams();
            int width = layoutParams.width;
            int height = layoutParams.height;
            int coverSize = (width > height ? height : width)/2;
            String coverPath = UrlUtils.getCoverPath(data.getPict_url(), coverSize);
            Glide.with(itemView.getContext()).load(coverPath).placeholder(R.drawable.glide_loading).into(cover);
            LogUtil.d(TAG,"URL: " + coverPath);
            //优惠券价格
            /**
             * 奇奇怪怪的知识增加了
             */
            preferentialText.setText(String.format(itemView.getContext().getString(R.string.text_goods_off_prise),data.getCoupon_amount()));
            //券后价
            String price = data.getZk_final_price();
            if (price != null) {
                float finalPrice = Float.parseFloat(price) - (float) (data.getCoupon_amount());
                priceText.setText(String.format("%.2f",finalPrice));
                originalPrise.setText(String.format(itemView.getContext().getString(R.string.text_goods_original_prise),price));
            }
            volumeText.setText(data.getVolume()+"");

            mLinearLayout.setOnClickListener(v ->{
                mListener.onItemClick(data);
            });
        }
    }

    public interface OnItemClickListener{
        void onItemClick(CategoriesById.DataBean data);
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        this.mListener = listener;
    }
}
